//
//  MHNearByCollectionViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/9/21.
//  Copyright © 2018年 acgera. All rights reserved.
//

#import "MHNearByCollectionViewController.h"
#import "MHNearByCollectionViewCell.h"
#import <AMapLocationKit/AMapLocationKit.h>
#import "MHNearCollectionViewModel.h"
#import "UIScrollView+MHRefresh.h"
#import "MHLocationManager.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
@interface MHNearByCollectionViewController ()<AMapLocationManagerDelegate>
@property (nonatomic, strong) AMapLocationManager *locationManager;
@property(nonatomic, assign) BOOL authorizationAccess;

@property(nonatomic, strong) MHNearCollectionViewModel *viewModel;
@end

@implementation MHNearByCollectionViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    [AMapServices sharedServices].apiKey = KeyForGaoDe;
    [self.collectionView mh_registerNibCell:MHNearByCollectionViewCell.class];
    self.locationManager = [[AMapLocationManager alloc]init];
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation]; //定位功能;
    self.collectionView.backgroundColor = MHColorFromHexString(@"#F8F8F8");
    //    AMapLocationManager
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)amapLocationManager:(AMapLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
{
    if(status == kCLAuthorizationStatusNotDetermined || status == kCLAuthorizationStatusRestricted || status == kCLAuthorizationStatusDenied)
    {
        self.authorizationAccess = NO;
        self.viewModel.dataSource = @[];
        self.viewModel.shouldPullDownToRefresh = NO;
    }else
    {
        self.authorizationAccess = YES;
        self.viewModel.shouldPullDownToRefresh = YES;
        [self.collectionView reloadData];
        [self.locationManager startUpdatingLocation];
        
    }
}



- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIImage imageNamed:self.authorizationAccess ?  @"globle_emptyData":@"img_found_location" ];
}


- (UIColor *)backgroundColorForEmptyDataSet:(UIScrollView *)scrollView
{
    return [UIColor whiteColor];
}


- (BOOL)emptyDataSetShouldAllowScroll:(UIScrollView *)scrollView {
    return YES;
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView
{
    NSString *text = self.authorizationAccess ? @"暂无附近数据" : @"需要开启定位才能使用附近功能~";
    
    NSDictionary *attributes = @{NSFontAttributeName: MHRegularFont_15,
                                 NSForegroundColorAttributeName: MHMetionTextColor};
    
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}




- (CAAnimation *)imageAnimationForEmptyDataSet:(UIScrollView *)scrollView
{
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath: @"transform"];
    
    animation.fromValue = [NSValue valueWithCATransform3D:CATransform3DIdentity];
    animation.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeRotation(M_PI_2, 0.0, 0.0, 1.0)];
    
    animation.duration = 0.25;
    animation.cumulative = YES;
    animation.repeatCount = MAXFLOAT;
    
    return animation;
}

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location
{
    self.viewModel.location = location;
    [self.viewModel.requestRemoteDataCommand execute:@(1)];
    [MHLocationManager uploadLocationToRemote:location];
    [self.locationManager stopUpdatingLocation];
    
}



- (NSAttributedString *)buttonTitleForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    if (!self.authorizationAccess) {
        NSString *text = nil;
        UIFont *font = nil;
        UIColor *textColor = nil;
        text = @"开启定位";
        font = MHRegularFont_13;
        textColor = MHColorFromHexString(@"#FE1126");
        NSMutableDictionary *attributes = [NSMutableDictionary new];
        if (font) [attributes setObject:font forKey:NSFontAttributeName];
        if (textColor) [attributes setObject:textColor forKey:NSForegroundColorAttributeName];
        return [[NSAttributedString alloc] initWithString:text attributes:attributes];
    }else
    {
        return nil;
    }
    
}
- (void)emptyDataSet:(UIScrollView *)scrollView didTapButton:(UIButton *)button
{
    [[MHLocationManager sharedInstance] openLocationSetting];
}


- (UIImage *)buttonBackgroundImageForEmptyDataSet:(UIScrollView *)scrollView forState:(UIControlState)state
{
    if (!self.authorizationAccess) {
        UIEdgeInsets capInsets = UIEdgeInsetsMake(10.0, 10.0, 10.0, 10.0);
        UIEdgeInsets rectInsets = UIEdgeInsetsZero;
        capInsets = UIEdgeInsetsMake(25.0, 25.0, 25.0, 25.0);
        rectInsets = UIEdgeInsetsMake(0.0, -100, 0.0, -100);
        
        UIImage *image =    [[[UIImage imageNamed:@"btn_circle_openposition"] resizableImageWithCapInsets:capInsets resizingMode:UIImageResizingModeStretch] imageWithAlignmentRectInsets:rectInsets];
         return  image;
    }else
    {
        return nil;
    }
}


-(void)configurelayout:(CHTCollectionViewWaterfallLayout *)layout
{
    //    layout.sectionInset = UIEdgeInsetsMake(0, 15, 25, 15);
    layout.headerHeight = 10;
    layout.footerHeight = 5;
    layout.columnCount = 2;
    layout.minimumColumnSpacing = 5;
    //    layout.minimumInteritemSpacing = 30;
}

//- (UIEdgeInsets)contentInset{
//    return UIEdgeInsetsMake(0, 0, 0, 0);
//}

- (void)configureCell:(MHNearByCollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(id)object
{
    [cell bindViewModel:object];
}


- (UICollectionViewCell *)collectionView:(MHCollectionView *)collectionView dequeueReusableCellWithIdentifier:(NSString *)identifier forIndexPath:(NSIndexPath *)indexPath
{
    MHNearByCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass(MHNearByCollectionViewCell.class) forIndexPath:indexPath];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //    MHHomeDiscoverItemViewModel *itemViewModel =  self.viewModel.dataSource[indexPath.item];
    return CGSizeMake((MH_SCREEN_WIDTH - 15)/2  , 220);
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
